package com.synergy.stockflow.model;

import com.synergy.stockflow.constant.ResultCode;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
public class Result<T> implements Serializable {
    private int code;
    private String message;
    private transient T data;

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCode.SUCCESS.value(), ResultCode.SUCCESS.getDesc(), data);
    }

    public static <T> Result<T> success(T data, String message) {
        return new Result<>(ResultCode.SUCCESS.value(), message, data);
    }

    public static <T> Result<T> error(int code, String message) {
        return new Result<>(code, message, null);
    }

    public static <T> Result<T> error(int code, String message, T data) {
        return new Result<>(code, message, data);
    }

    // 修复fail方法：参数名改为合法名称，添加返回语句
    public static Result<String> fail(String message, Object object) {
        // 使用错误码常量更规范，假设500是默认错误码
        return new Result<>(ResultCode.ERROR.value(), message, null);
    }

    private Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
}
